package com.ivvi.autoreco;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

import com.ivvi.autoreco.model.LocalImage;
import com.ivvi.autoreco.widget.StickyGridHeadersGridView;

import org.opencv.ivvi.custom.Constants;
import org.opencv.ivvi.custom.CropType;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends AppCompatActivity {

    private Button generateBtn;
    private StickyGridHeadersGridView gridView;
    private TextView tvMsg;
    private DoneAdapter doneAdapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

//        if (!OpenCVLoader.initDebug()) {
//            Log.e(this.getClass().getSimpleName(), "  OpenCVLoader.initDebug(), not working.");
//        } else {
//            Log.d(this.getClass().getSimpleName(), "  OpenCVLoader.initDebug(), working.");
//        }

        generateBtn = (Button) findViewById(R.id.btn_generate);
        generateBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                reco();
            }
        });

        gridView = (StickyGridHeadersGridView) findViewById(R.id.grid);
        tvMsg = (TextView) findViewById(R.id.tv_msg);

        if (doneAdapter == null) {
            doneAdapter = new DoneAdapter(this);
        }
        gridView.setLoadData(false);
        gridView.setAdapter(doneAdapter);

        gridView.setOnItemClickListener(itemClickListener);
    }


    private boolean started = false;

    private void reco() {
        if (started) {
            Toast.makeText(this, "正在智能识别...", Toast.LENGTH_SHORT).show();
            return;
        }
        BusinessCenter businessCenter = BusinessCenter.getInstance();
        started = true;
        businessCenter.setBusinessListener(onBusinessListener);
        businessCenter.start(this);
    }

    private BusinessCenter.OnBusinessListener onBusinessListener = new BusinessCenter.OnBusinessListener() {
        @Override
        public void onStart() {
            runOnUiThread(new Runnable() {
                @Override
                public void run() {
                    Toast.makeText(MainActivity.this, "智能识别开始", Toast.LENGTH_SHORT).show();
                }
            });
        }

        @Override
        public void onReco(final LocalImage localImage) {
            runOnUiThread(new Runnable() {
                @Override
                public void run() {
                    tvMsg.setVisibility(View.VISIBLE);
                    tvMsg.setText("正在识别：" + localImage.getAbsPath());
                }
            });
        }

        @Override
        public void onResult(final LocalImage localImage, final double[] d, final long spendTime) {

            runOnUiThread(new Runnable() {
                @Override
                public void run() {
                    GridItem gridItem = generate(localImage, d, spendTime);
                    gridItems.add(gridItem);
                }
            });
        }

        @Override
        public void onFinish(final int total, final long spendTime) {
            started = false;
            runOnUiThread(new Runnable() {
                @Override
                public void run() {
                    tvMsg.setVisibility(View.VISIBLE);
                    tvMsg.setText("Intelligent identification total:" + total + ",spend time:" + spendTime);
                    doneAdapter.setItems(gridItems);
                }
            });
        }
    };

    private List<GridItem> gridItems = new ArrayList<>();

    private final String s3d = new String("图片");
//    private String s2d = new String("S2D");

    private GridItem generate(LocalImage localImage, double[] d, long spendTime) {

        synchronized (s3d) {
            GridItem item = new GridItem();
            item.setId(localImage.get_id());
            item.setAbsPath(localImage.getAbsPath());
            item.setTime(spendTime);
            item.setsDegree(d);

            if (d[0] >= Constants.Standard) {
                item.setCropType(CropType.LEFT);
                item.setHeadId(s3d);
            } else if (d[1] >= Constants.Standard) {
                item.setCropType(CropType.TOP);
                item.setHeadId(s3d);
            } else {
                item.setCropType(CropType.DEFAULT);
                item.setHeadId(s3d);
            }

            return item;
        }

    }

    private AdapterView.OnItemClickListener itemClickListener = new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

            Object object = view.getTag();
            if (object == null) {
                return;
            }

            GridItem gridItem = (GridItem) object;
            Toast.makeText(MainActivity.this, gridItem.toString(), Toast.LENGTH_SHORT).show();
        }
    };


}
